home *** CD-ROM | disk | FTP | other *** search
-
- Listing 2
-
- //
- // rational.cpp
- //
- #include "mylib.h"
- #include "rat1.h"
-
- rational &rational::operator+=(rational r)
- {
- num = num * r.denom + r.num * denom;
- denom *= r.denom;
- simplify();
- return *this;
- }
-
- rational &rational::operator-=(rational r)
- {
- num = num * r.denom - r.num * denom;
- denom *= r.denom;
- simplify();
- return *this;
- }
-
- rational &rational::operator*=(rational r)
- {
- num *= r.num;
- denom *= r.denom;
- simplify();
- return *this;
- }
-
- rational &rational::operator/=(rational r)
- {
- num *= r.denom;
- denom *= r.num;
- simplify();
- return *this;
- }
-
- rational rational::operator+(rational r)
- {
- rational result(*this);
- return result += r;
- }
-
- rational rational::operator-(rational r)
- {
- rational result(*this);
- return result -= r;
- }
-
- rational rational::operator*(rational r)
- {
- rational result(*this);
- return result *= r;
- }
-
- rational rational::operator/(rational r)
- {
- rational result(*this);
- return result /= r;
- }
-
- void rational::put(FILE *f)
- {
- fprintf(f, "(%ld/%ld)", num, denom);
- }
-
- void rational::simplify()
- {
- long x = gcd(num, denom);
- num /= x;
- denom /= x;
- }
-
-